/*
 * @Description: 
 * @Author: changfeng
 * @brief: 
 * @version: 
 * @Date: 2025-03-18 06:09:59
 * @LastEditors:  
 * @LastEditTime: 2025-03-18 06:10:23
 */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/input.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>

#define DEVICE_PATH "/dev/input/"

void print_device_info(const char *device_path) {
    int fd = open(device_path, O_RDONLY);
    if (fd == -1) {
        perror("Unable to open device");
        return;
    }

    char name[256] = "Unknown";
    if (ioctl(fd, EVIOCGNAME(sizeof(name)), name) == -1) {
        perror("Unable to get device name");
        close(fd);
        return;
    }

    struct input_id device_info;
    if (ioctl(fd, EVIOCGID, &device_info) == -1) {
        perror("Unable to get device ID");
        close(fd);
        return;
    }

    printf("Device Path: %s\n", device_path);
    printf("  Name: %s\n", name);
    printf("  Vendor ID: 0x%04x\n", device_info.vendor);
    printf("  Product ID: 0x%04x\n", device_info.product);
    printf("  Version ID: 0x%04x\n", device_info.version);

    close(fd);
}

void list_input_devices() {
    DIR *dir = opendir(DEVICE_PATH);
    if (dir == NULL) {
        perror("Unable to open /dev/input directory");
        return;
    }

    struct dirent *entry;
    while ((entry = readdir(dir)) != NULL) {
        // Check if the directory entry is a device file (starts with "event")
        if (strncmp(entry->d_name, "event", 5) == 0) {
            char device_path[512];
            snprintf(device_path, sizeof(device_path), "%s%s", DEVICE_PATH, entry->d_name);
            print_device_info(device_path);
        }
    }

    closedir(dir);
}

int main() {
    list_input_devices();
    return 0;
}
